package com.hunttown.mes.manage.service;

import com.hunttown.common.domain.Page;
import com.hunttown.mes.common.utils.FormatUtils;
import com.hunttown.mes.rpc.api.WorkflowApplyLineRpcService;
import com.hunttown.mes.rpc.domain.WorkflowApplyLineDTO;
import com.hunttown.mes.rpc.domain.query.WorkflowApplyLineDTOQuery;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * created by wangjunfu on 2019-08-01 16:55:16
 */
@Service
public class WorkflowApplyLineManageService {

    private final static Logger logger = LoggerFactory.getLogger(WorkflowApplyLineManageService.class);

    private final WorkflowApplyLineRpcService rpcService;

    @Autowired
    public WorkflowApplyLineManageService(WorkflowApplyLineRpcService rpcService) {
        this.rpcService = rpcService;
    }

    // 新增
    public WorkflowApplyLineDTO insert(WorkflowApplyLineDTO objDTO) {
        return rpcService.insert(objDTO);
    }

    // 通过ID修改
    public Boolean updateInfoById(WorkflowApplyLineDTO objDTO) {
        return rpcService.updateInfoById(objDTO);
    }

    // 通过Query修改
    public Boolean updateInfoByQuery(WorkflowApplyLineDTOQuery objDTOQuery) {
        return rpcService.updateInfoByQuery(objDTOQuery);
    }

    // 通过ID删除
    public Boolean delete(Integer id) {
        return rpcService.deleteById(id);
    }

    // 批量删除
    public Boolean deleteForBatch(String clearIds) {

        //处理 “,5,6,7,” 为List
        List<Integer> idList = FormatUtils.commaToList_Integer(clearIds);

        if (idList.size() == 0) {
            return false;
        }

        WorkflowApplyLineDTOQuery query = new WorkflowApplyLineDTOQuery();
        query.setDeleteFlag(1);
        query.setIds(idList);

        return rpcService.updateInfoByQuery(query);
    }

    // 通过ID获取
    public WorkflowApplyLineDTO getById(Integer id) {
        if (id <= 0) {
            return null;
        }
        return rpcService.selectById(id);
    }

    // 通过Query获取
    public WorkflowApplyLineDTO getByQuery(WorkflowApplyLineDTOQuery objDTOQuery) {
        return rpcService.selectObjByQuery(objDTOQuery);
    }

    // 获取分页列表
    public Page<WorkflowApplyLineDTO> getForPage(WorkflowApplyLineDTOQuery objDTOQuery) {
        return rpcService.getListForPage(objDTOQuery);
    }

    // 环节审批
    public Boolean changeState(Integer id, Integer state, String remark, Integer approver) {
        WorkflowApplyLineDTO obj = new WorkflowApplyLineDTO();
        obj.setIsCurrent(0);    //当前节点设置为0
        obj.setStatus(state);   //改变审批状态
        obj.setRemark(remark);  //审批意见
        obj.setRealApproverId(approver);
        obj.setRealApproverTime(new Date());
        obj.setId(id);
        return updateInfoById(obj);
    }

    /**
     * 通过审批ID和环节顺序获取环节ID
     *
     * @param businessId 业务ID
     * @param applyId    审批ID
     * @param nodeSign   环节标记
     * @return
     */
    public Integer getLineIdByNodesign(Integer businessId, Integer applyId, Integer nodeSign) {
        Integer lineId = 0;

        WorkflowApplyLineDTOQuery query = new WorkflowApplyLineDTOQuery();
        query.setBusinessId(businessId);
        query.setApplyId(applyId);
        query.setNodeSign(nodeSign);
        query.setStatus(0); //限制未审批，因为可能会有加签环节
        WorkflowApplyLineDTO dtoLine = getByQuery(query);
        if (dtoLine != null) {
            lineId = dtoLine.getId();
        }

        return lineId;
    }

    /**
     * 获取当前最新审批环节
     *
     * @param applyId 审批ID
     * @return
     */
    public Integer getCurrLineIdByApplyId(Integer applyId) {
        Integer lineId = 0;

        WorkflowApplyLineDTOQuery query = new WorkflowApplyLineDTOQuery();
        query.setApplyId(applyId);
        query.setIsCurrent(1);
        WorkflowApplyLineDTO dtoLine = getByQuery(query);
        if (dtoLine != null) {
            lineId = dtoLine.getId();
        }

        return lineId;
    }

    /**
     * 获取当前最新审批环节序号
     *
     * @param applyId 审批ID
     * @return
     */
    public Integer getCurrLineNodeSignIdByApplyId(Integer applyId) {
        Integer nodeSign = 0;

        WorkflowApplyLineDTOQuery query = new WorkflowApplyLineDTOQuery();
        query.setApplyId(applyId);
        query.setIsCurrent(1);
        WorkflowApplyLineDTO dtoLine = getByQuery(query);
        if (dtoLine != null) {
            nodeSign = dtoLine.getNodeSign();
        }

        return nodeSign;
    }

    /**
     * 获取当前最新审批环节标题
     *
     * @param applyId 审批ID
     * @return
     */
    public String getCurrLineTitleByApplyId(Integer applyId) {
        String lineTitle = "未知";

        WorkflowApplyLineDTOQuery query = new WorkflowApplyLineDTOQuery();
        query.setApplyId(applyId);
        query.setIsCurrent(1);
        WorkflowApplyLineDTO dtoLine = getByQuery(query);
        if (dtoLine != null) {
            lineTitle = dtoLine.getNodeTitle();
        }

        return lineTitle;
    }

    /**
     * 获取当前最新环节信息
     *
     * @param applyId 审批ID
     * @return
     */
    public WorkflowApplyLineDTO getCurrLineDTOByApplyId(Integer applyId) {
        WorkflowApplyLineDTOQuery query = new WorkflowApplyLineDTOQuery();
        query.setApplyId(applyId);
        query.setIsCurrent(1);
        return getByQuery(query);
    }

    /**
     * 获取第一个审批环节ID
     *
     * @param applyId 审批ID
     * @return
     */
    public Integer getFirstLineIdByApplyId(Integer applyId) {
        Integer lineId = 0;

        WorkflowApplyLineDTOQuery query = new WorkflowApplyLineDTOQuery();
        query.setApplyId(applyId);
        query.setOrderBy("order by node_sort asc");
        WorkflowApplyLineDTO dtoLine = getByQuery(query);
        if (dtoLine != null) {
            lineId = dtoLine.getId();
        }

        return lineId;
    }
}